package com.ykdl.member.kid.set;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.ConsigneeListBean;
import com.ykdl.member.kid.beans.ResultBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.marketcard.CardPersonalInformationActivity;
import com.ykdl.member.kid.regandlogin.PerfectUserInfoActivity;
import com.ykdl.member.kid.set.MyAddressActivity.getDataTag;
import com.ykdl.member.views.MyWheelView;
import com.ykdl.member.views.MyWheelView.OnWheelViewDataChanger;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class AddAddressActivity extends BaseScreen implements OnClickListener{
	private  EditText phone_number,receiver_name,address;
	private MyWheelView location;
	private TextView region ;
	private boolean iopen = false ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("添加收货地址", 0, R.drawable.back_bg, new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		}, R.string.hmc_bc,
		0, new OnClickListener() {

			@Override
			public void onClick(View v) {
				postData();
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.addaddressactivity_xml);
		inint();
	}
	public void inint() {
		region = (TextView)findViewById(R.id.region);
		region.setOnClickListener(this);
		receiver_name = (EditText)findViewById(R.id.receiver_name);
		address = (EditText)findViewById(R.id.address);
		phone_number = (EditText)findViewById(R.id.phone_number);
		phone_number.setInputType(EditorInfo.TYPE_CLASS_PHONE);
		location = (MyWheelView) findViewById(R.id.select_location_layout);
		location.setWheelView(MyWheelView.CHINA);
		iopen = true;
//		location.setVisibility(View.INVISIBLE);
		location.setInVisible(true);
		location.setMyVisible();
		location.findViewById(R.id.my_wheelview_cancel).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				iopen = false;
				location.setMyGone();
			}
		});
		location.setOnWheelViewDataChanger(new OnWheelViewDataChanger() {
			@Override
			public void updataByChanger(String data) {
				if (!iopen) {
					InputMethodManager mInputMethodManager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
					mInputMethodManager.toggleSoftInput(0, InputMethodManager.RESULT_UNCHANGED_SHOWN);
					
				} else {
					QLog.debug("data", data);
					String[] datas = data.split("&#&");
					String[] contents = datas[0].split(",");
					String result = "";
					for (int i = 0; i < contents.length; i++) {
						if (!"".equals(result)) {
							result += ",";
						}
						result += contents[i];
					}
					region.setText(result);
				}
			
				}
		});
	}
	public void postData() {
		
		String  pageurl =KidConfig.ADD_CONSIGNEE;
		if (receiver_name.getText().toString().trim().length()==0) {
			Toast.makeText(mContext, "请填写收货人姓名", 1).show();
			return;
		}
		if (phone_number.getText().toString().trim().length()!=11) {
			Toast.makeText(mContext, "您输入的手机号错误,请输入11位手机号", 1).show();
			return;
		}
		if (region.getText().toString().trim().length()==0) {
			Toast.makeText(mContext, "请选择所在区域", 1).show();
			return;
		}
		if (address.getText().toString().trim().length()==0) {
			Toast.makeText(mContext, "请详细的地址,如街道,门牌号等", 1).show();
			return;
		}
		
		showProgress("保存中请稍等...");
		String name="";
		try {
			name = URLEncoder.encode(receiver_name.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String area="";
		try {
			area = URLEncoder.encode(region.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String addresss="";
		try {
			addresss = URLEncoder.encode(address.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String phone_numbers="";
		try {
			phone_numbers = URLEncoder.encode(phone_number.getText().toString().trim(), "utf8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("name", name);
		parameterss.put("phone",phone_numbers);
		parameterss.put("area", area);
		parameterss.put("address", addresss);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss,null);
		 TaskManager.startHttpRequest(request, new postDataTag(),  ResultBean.class);
	}
	
	class postDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof ResultBean) {
				ResultBean bean = (ResultBean)t;
				if (null==bean.getError()) {
					Toast.makeText(mContext, "添加成功", 1).show();
					AddAddressActivity.this.finish();
				}else {
					Toast.makeText(mContext, "添加失败,"+bean.getDesc(), 1).show();
				}
				}
				
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.region:
			iopen = true;
			location.setVisibility(View.VISIBLE);
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(AddAddressActivity.this
							.getCurrentFocus().getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
			location.setInVisible(true);
			location.setMyVisible();
			break;

		default:
			break;
		}
	}
}
